const { regeneratorRuntime, md5 } = global

Page(global.connect(
  state => ({
    ...state.homeReducer,
    ...state.globalReducer
  }),
  dispatch => ({
    handleRoute: global.handleRoute,
    async initHome(data) {
      await dispatch(global.homeReducer.initHome(data))
    },
    async getMinaAuthAndUserInfo(data) {
      await dispatch(global.homeReducer.getMinaAuthAndUserInfo(data))
    },
    async handleSubmit(e) {
      console.log('handleSubmit', e)

      const { mobile, password } = e.detail.value
      if (mobile.length !== 11 || !password.length) {
        this.selectComponent('#toast').handleShow({
          message: '请填写完整的账户信息'
        })
        return
      }
      await dispatch(global.globalReducer.minaLogin({
        mobile,
        password: md5(password)
      }))

      if (this.data.minaLoginRes.actionStatus === 'SUCCESS') {
        this.handleRoute({ route: 'index', type: 'redirectTo' })
      } else {
        this.selectComponent('#toast').handleShow({
          message: this.data.minaLoginRes.apiMes || '系统异常'
        })
      }

    },
    handleReset(e) {
      console.log('handleReset', e)
    },
    handleGetUserInfo(e) {
      console.log('handleGetUserInfo', e)
      if (e.detail.errMsg === 'getUserInfo:ok') {
        dispatch(global.globalReducer.setMinaUserInfo(e.detail))
      } else {
        // todo
      }
    }
  })
)({
  data: {
    mobileFocus: false,
    passwordFocus: false,
  },
  onLoad(options) {
    console.log('Home onLoad')
  },
  async onReady() {

    await this.initHome('INIT_HOME_VALUE')

    console.log('Home onReady')

    console.groupEnd()
  },
  handleBugInput(e) {
    console.log('handleBugInput 尝试解决ios 11.0.1版本聚焦无法弹出键盘问题')
  },
  handleBug(e) {
    console.log('handleBug')
    const { field } = e.currentTarget.dataset
    switch (field) {
      case 'mobile':
        {
          this.setData({
            mobileFocus: !this.data.mobileFocus,
            passwordFocus: false
          })
        }
        break;
      case 'password':
        {
          this.setData({
            mobileFocus: false,
            passwordFocus: !this.data.passwordFocus
          })
        }
        break;
      default:
        break;
    }
  }
}))
